//
//  Offer_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/12.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "Offer_ViewController.h"
#import "Masonry.h"
#import "BaseLabel.h"
#import "BaseButton.h"
#import "BaseTextField.h"
#import "BaseView.h"
#import "AddOfferViewController.h"
#import "BaseTableView.h"
#import "OfferTableViewCell.h"
#import "DLWNetworkingParser.h"
#import "OfferEggModel.h"
#import "DLDatePickerView.h"
@interface Offer_ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTextField *textFieldStart;
@property (nonatomic, strong) BaseTextField *textFieldEnd;
@property (nonatomic, strong) BaseButton *buttonInquiry;
@property (nonatomic, strong) BaseButton *buttonRefresh;
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrData;

@property (nonatomic, strong) DLDatePickerView *datePickerView;
@end

@implementation Offer_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _arrData = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithWhite:0.939 alpha:1.000];
    self.navigationItem.title = _navigationTitle;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"报价" style:UIBarButtonItemStyleDone target:self action:@selector(offerItemAction)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self handleData];
    [self createSubView];
}
#pragma mark - 处理数据
- (void)handleData
{
    [_arrData removeAllObjects];
    [DLWNetworkingParser GETWithURL:@"http://www.danjiguanjia.com/api/Price/getAllEggPrice?uid=9520&page=1" par:nil success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"data"][@"data"]) {
            OfferEggModel *model = [OfferEggModel modelWithDic:dic];
            [_arrData addObject:model];
            [_tableView reloadData];
        }
    } filed:^(NSError *error) {
        
    }];
}


#pragma mark -创建视图
- (void)createSubView
{
    // 创建tableView
    _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[OfferTableViewCell class] forCellReuseIdentifier:@"reuse"];

    
    _textFieldStart = [[BaseTextField alloc] init];
    _textFieldStart.placeholder = @"开始日期";
    _textFieldStart.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textFieldStart];
    
    _textFieldEnd = [[BaseTextField alloc] init];
    _textFieldEnd.borderStyle = UITextBorderStyleRoundedRect;
    _textFieldEnd.placeholder = @"结束日期";
    [self.view addSubview:_textFieldEnd];
    
    _buttonInquiry = [BaseButton buttonWithType:UIButtonTypeSystem];
    [_buttonInquiry setTitle:@"查询" forState:UIControlStateNormal];
    _buttonInquiry.backgroundColor = [UIColor redColor];
    _buttonInquiry.layer.masksToBounds = YES;
    _buttonInquiry.layer.cornerRadius = 4;
    // 添加事件
    [_buttonInquiry addTarget:self action:@selector(buttonInquiryAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonInquiry];
    
    _buttonRefresh = [BaseButton buttonWithType:UIButtonTypeSystem];
    [_buttonRefresh setTitle:@"刷新" forState:UIControlStateNormal];
    _buttonRefresh.backgroundColor = [UIColor blueColor];
    _buttonRefresh.layer.masksToBounds = YES;
    _buttonRefresh.layer.cornerRadius = 4;
    [self.view addSubview:_buttonRefresh];
    [_buttonRefresh addTarget:self action:@selector(buttonRefrechAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 布局
    [_textFieldStart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(10);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(30);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.27);
        make.height.mas_equalTo(30);
    }];
    
    [_textFieldEnd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(10);
        make.left.mas_equalTo(_textFieldStart.mas_right).mas_offset(8);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.27);
        make.height.mas_equalTo(30);
    }];
    
    [_buttonInquiry mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(10);
        make.left.mas_equalTo(_textFieldEnd.mas_right).mas_offset(8);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.13);
        make.height.mas_equalTo(30);
    }];
    
    [_buttonRefresh mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(10);
        make.left.mas_equalTo(_buttonInquiry.mas_right).mas_offset(8);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.13);
        make.height.mas_equalTo(30);
    }];
    
    UIView *Backview = [[UIView alloc] init];
    Backview.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    [self.view addSubview:Backview];
    
    BaseLabel *lableDate = [[BaseLabel alloc] init];
    lableDate.text = @"记录日期";
    lableDate.textColor = [UIColor whiteColor];
    [self.view addSubview:lableDate];
    
    BaseLabel *lablePlace = [[BaseLabel alloc] init];
    lablePlace.text = @"地区";
    lablePlace.textColor = [UIColor whiteColor];
    [self.view addSubview:lablePlace];

    BaseLabel *lablePrice = [[BaseLabel alloc] init];
    lablePrice.text = @"价格";
    lablePrice.textColor = [UIColor whiteColor];
    [self.view addSubview:lablePrice];
    
    BaseLabel *lableOperate = [[BaseLabel alloc] init];
    lableOperate.text = @"操作";
    lableOperate.textColor = [UIColor whiteColor];
    [self.view addSubview:lableOperate];
    
    [Backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buttonRefresh.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(0);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(50);
    }];
    
    [lableDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Backview.mas_top).mas_offset(5);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.width.mas_equalTo(Backview.mas_width).multipliedBy(0.2);
        make.bottom.mas_equalTo(Backview.mas_bottom).mas_equalTo(-5);
    }];

    [lablePlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Backview.mas_top).mas_offset(5);
        make.left.mas_equalTo(lableDate.mas_right).mas_offset(50);
        make.width.mas_equalTo(Backview.mas_width).multipliedBy(0.11);
        make.bottom.mas_equalTo(Backview.mas_bottom).mas_equalTo(-5);
    }];

    [lablePrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Backview.mas_top).mas_offset(5);
        make.left.mas_equalTo(lablePlace.mas_right).mas_offset(50);
        make.width.mas_equalTo(Backview.mas_width).multipliedBy(0.11);
        make.bottom.mas_equalTo(Backview.mas_bottom).mas_equalTo(-5);
    }];

    [lableOperate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Backview.mas_top).mas_offset(5);
        make.left.mas_equalTo(lablePrice.mas_right).mas_offset(50);
        make.width.mas_equalTo(Backview.mas_width).multipliedBy(0.11);
        make.bottom.mas_equalTo(Backview.mas_bottom).mas_equalTo(-5);
    }];

}
#pragma mark - tableView 的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OfferTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    OfferEggModel *model = _arrData[indexPath.row];
    cell.model = model;
    cell.blockButton = ^(OfferEggModel *model)
    {
        AddOfferViewController *AddVC = [[AddOfferViewController alloc] init];
        
        AddVC.model = model;
        [self.navigationController pushViewController:AddVC animated:YES];
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}


#pragma mark - 报价
- (void)offerItemAction
{
    // 推出添加价格的界面
    AddOfferViewController *addOfferVC = [[AddOfferViewController alloc] init];
    addOfferVC.navigationTitle = @"添加鸡蛋报价";
    [self.navigationController pushViewController:addOfferVC animated:YES];
}

#pragma mark - 查询的点击事件
- (void)buttonInquiryAction:(BaseButton *)button
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"9520",@"uid", @"1", @"page", _textFieldStart.text,@"starttime", _textFieldEnd.text, @"endtime", nil];
    [DLWNetworkingParser GETWithURL:@"http://www.danjiguanjia.com/api/Price/getAllEggPrice" par:dic success:^(id responseObject) {
        [_arrData removeAllObjects];
        for (NSDictionary *dic in responseObject[@"data"][@"data"]) {
            OfferEggModel *model = [OfferEggModel modelWithDic:dic];
            [_arrData addObject:model];
            [_tableView reloadData];
        }
    } filed:^(NSError *error) {
        
    }];
}

-(void)buttonRefrechAction:(BaseButton *)button
{
    [self handleData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
